import java.util.Random;
import java.util.Scanner;


public class Nim {
	
	public static int computerAI(int matches, final int MAXCOUNT){
		if (matches > MAXCOUNT + 1 ){
			if ((matches % (MAXCOUNT + 1)) == 0) return --matches;
			else return matches -= matches % (MAXCOUNT + 1);
		}
		else return matches -= matches;
		 
	}
	
	public static void main(String[] args) {
		
		final int MAXCOUNT = 5;
		int matches = 18;
		
		Scanner input = new Scanner(System.in);
		Random random = new Random();
		boolean firstStep = random.nextBoolean();

		while(matches>0){
			if(firstStep){
				System.out.println("Player input: ");
				matches -=input.nextInt();
				firstStep=false;
			}else{
			System.out.println("Computer input: "+ ( matches - computerAI(matches, MAXCOUNT) ));
			matches = computerAI(matches, MAXCOUNT);
			firstStep=true;
			}
			
			System.out.println("Remaining matches " +  matches );
		
		}
		
		if(firstStep) System.out.println("Computer WIN!");
		else System.out.println("Player WIN!");
		
		
		
		
	}

}
